package com.zz.controller;

import com.zz.common.Log;
import com.zz.common.R;
import com.zz.service.QiniuService;
import com.zz.utils.QiniuUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.qiniu.storage.model.DefaultPutRet;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * 七牛云工具类
 * @author linfen
 * @since 2020-04-8
 */
@Slf4j
@RestController
@RequestMapping("/common")
@Validated
public class QiNiuController {

    @Autowired
    private QiniuUtil qiniuUtil;

    @Autowired
    private QiniuService qiniuService;
    /**
     * 七牛云文件上传
     *
     * @param file 文件
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public R upload(MultipartFile file) {
        return qiniuService.upload(file);
    }

    /**
     * 上传头像
     * */
    @PostMapping("/uploadUser")
    @Log(title = "用户上传头像", dataLocationType = "link")
    R<String> uploadUser(MultipartFile file, HttpServletRequest request){
        return qiniuService.uploadUser(file,request);
    }

    /**
     * 社团上传图片
     * */
    @PostMapping("/uploadClub")
    @Log(title = "上传社团图片", dataLocationType = "link")
    R<String> uploadClub(int clubId, MultipartFile file){
        return qiniuService.uploadClub(clubId,file);
    }

    /**
     * 上传活动图片
     * */
    @PostMapping("/uploadActivity")
    @Log(title = "上传活动图片", dataLocationType = "link")
    public R<String> uploadActivity(@NotNull(message = "活动编码不能为空") int activityId, MultipartFile file){
        return qiniuService.uploadActivity(activityId, file);
    }

    /**
     * 获取社团图片url
     * */
    @GetMapping("/getClubUrl")
    @Log(title = "获取社团图片url", dataLocationType = "link")
    R<String> getClubUrl(@NotNull(message = "社团编码不能为空")int clubId){
        return qiniuService.getClubUrl(clubId);
    }

    /**
     * 获取头像图片url
     * */
    @GetMapping("/getUserUrl")
    @Log(title = "获取头像图片url", dataLocationType = "link")
    R<String> getUserUrl(@NotNull(message = "用户学号不能为空")int userId){
        return qiniuService.getUserUrl(userId);
    }

    /**
     * 获取活动图片url
     * */
    @GetMapping("/getActivityUrl")
    @Log(title = "获取活动图片url", dataLocationType = "link")
    R<String> getActivityUrl(@NotNull(message = "活动编码不能为空")int activityId) {
        return qiniuService.getActivityUrl(activityId);
    }


    /**
     * 七牛云私有文件下载
     *
     * @param filename 文件名
     * @return
     */
    @RequestMapping("/private/file/{filename}")
    public void privateDownload(@PathVariable("filename") String filename, HttpServletResponse response) {
        if (filename.isEmpty()) {
            return;
        }

        try {
            String privateFile = qiniuUtil.getPrivateFile(filename);
            log.info("文件下载地址：" + privateFile);
            response.sendRedirect(privateFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 七牛云文件下载
     *
     * @param filename 文件名
     * @return
     */
    @RequestMapping("/file/{filename}")
    public void download(@PathVariable("filename") String filename, HttpServletResponse response) {
        if (filename.isEmpty()) {
            return;
        }

        try {
            String privateFile = qiniuUtil.getFile(filename);
            log.info("文件下载地址：" + privateFile);
            response.sendRedirect(privateFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 七牛云删除文件
     *
     * @param filename 文件名
     * @return
     */
    @Log(title = "删除七牛云文件",dataLocationType = "link")
    @RequestMapping("/delete")
    public R deleteFile(String filename, HttpServletResponse response) {
        if (filename.isEmpty()) {
            return R.error("未知文件");
        }

        try {
            boolean result = qiniuUtil.delete(filename);
            if (result) {
                return R.success("文件删除成功");
            } else{
                return R.error("文件删除失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("文件删除失败");
        }
    }
}